package com.jilefojing.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.jilefojing.beans.pojos.Tag;
import com.jilefojing.beans.query.TagQuery;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.dao.ITagDao;
import com.jilefojing.dao.base.impl.BaseDao;
import com.jilefojing.dao.mapper.ITagMapper;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年07月25日 22时00分52秒
 */
@Repository
public class TagDao extends BaseDao<Tag> implements ITagDao {
	@Autowired
	private ITagMapper tagMapper;

	@Override
	public List<Tag> getListByIds(List<Long> ids) {
		Example example = new Example(Tag.class);
		example.createCriteria().andIn(Tag.FIELD_ID, ids);
		return super.getListByExample(example);
	}

	@Override
	public Pagination<Tag> getPagination(TagQuery tagQuery, Pagination<Tag> pagination) {
		Example example = new Example(Tag.class);
		Criteria criteria = example.createCriteria();
		if (Detect.notEmpty(tagQuery.getIds())) {
			criteria.andIn(Tag.FIELD_ID, tagQuery.getIds());
		}
		if (Detect.notEmpty(tagQuery.getCreateUserIds())) {
			criteria.andIn(Tag.FIELD_CREATEUSER, tagQuery.getCreateUserIds());
		}
		if (Detect.notEmpty(tagQuery.getLike_name())) {
			criteria.andLike(Tag.FIELD_NAME, "%" + tagQuery.getLike_name() + "%");
		}
		if (Detect.notEmpty(tagQuery.getName())) {
			criteria.andEqualTo(Tag.FIELD_NAME, tagQuery.getName());
		}

		if (tagQuery.getEqualCreateTime() != null) {
			criteria.andEqualTo(Tag.FIELD_CREATETIME, tagQuery.getEqualCreateTime());
		}

		if (tagQuery.getEqualAndBeforCreateTime() != null) {
			criteria.andGreaterThanOrEqualTo(Tag.FIELD_CREATETIME, tagQuery.getEqualAndBeforCreateTime());
		}

		if (tagQuery.getEqualAndAfterCreateTime() != null) {
			criteria.andLessThanOrEqualTo(Tag.FIELD_CREATETIME, tagQuery.getEqualAndAfterCreateTime());
		}

		if (tagQuery.getBeforCreateTime() != null) {
			criteria.andGreaterThan(Tag.FIELD_CREATETIME, tagQuery.getBeforCreateTime());
		}

		if (tagQuery.getAfterCreateTime() != null) {
			criteria.andLessThan(Tag.FIELD_CREATETIME, tagQuery.getAfterCreateTime());
		}

		if (Detect.notEmpty(pagination.getOrders())) {
			example.setOrderByClause(pagination.getOrdersToStr());
		}

		return this.getPagination(pagination, example);
	}

	@Override
	public List<Tag> getListByName(String name) {
		Example example = new Example(Tag.class);
		example.createCriteria().andEqualTo(Tag.FIELD_NAME, name);
		return super.getListByExample(example);
	}
}
